package cn.tedu.sp06.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.util.JsonResult;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import jdk.internal.org.objectweb.asm.commons.JSRInlinerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;


import java.util.List;

@RestController
@Slf4j
public class RibbonController {

    @Autowired
    private RestTemplate restTemplate;

    //调用商品服务
    @GetMapping("/item-service/{orderId}")
    @HystrixCommand(fallbackMethod = "getItemsFB")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId){
        //调用远程的商品服务，获取商品列表
        return restTemplate.getForObject("http://item-service/{1}",JsonResult.class,orderId);
    }
    @PostMapping("/item-service/decreaseNumber")
    @HystrixCommand(fallbackMethod = "decreaseNumberFB")
    public JsonResult<?> decreaseNumber(@RequestBody List<Item> items){
        return restTemplate.postForObject("http://item-service/decreaseNumber",items,JsonResult.class);
    }

    //调用用户
    @GetMapping("/user-service/{userId}")
    @HystrixCommand(fallbackMethod = "getUserFB")
    public JsonResult<User> getUser(@PathVariable Integer userId){
        return restTemplate.getForObject("http://user-service/{1}",JsonResult.class,userId);
    }
    @GetMapping("/user-service/{userId}/score")
    @HystrixCommand(fallbackMethod = "addScoreFB")
    public JsonResult<User> addScore(@PathVariable Integer userId,Integer score){
        return restTemplate.getForObject(
                "http://user-service/{1}/score?score={2}",
                JsonResult.class,
                userId,
                score);
    }

    //调用订单
    @GetMapping("/order-service/{orderId}")
    @HystrixCommand(fallbackMethod = "getOrderFB")
    public JsonResult<Order> getOrder(@PathVariable String orderId){
        return restTemplate.getForObject("http://order-service/{1}",JsonResult.class,orderId);
    }
    @GetMapping("/order-service")
    @HystrixCommand(fallbackMethod = "saveOrderFB")
    public JsonResult<?> saveOrder(){
        return restTemplate.getForObject("http://order-service",JsonResult.class);
    }

    ////////////////////////////////////////////

    public JsonResult<List<Item>> getItemsFB( String orderId){
        /**
         * 1、错误提示
         * 2、缓存数据
         * 3、根据业务逻辑返回任意内容
         */
        return JsonResult.err().msg("调用商品失败");
    }
    public JsonResult<?> decreaseNumberFB( List<Item> items){
        return JsonResult.err().msg("调用商品失败");
    }
    public JsonResult<User> getUserFB( Integer userId){
        return JsonResult.err().msg("调用用户失败");
    }
    public JsonResult<User> addScoreFB( Integer userId,Integer score){
        return JsonResult.err().msg("调用用户失败");
    }
    public JsonResult<Order> getOrderFB( String orderId){
        return JsonResult.err().msg("调用订单失败");
    }
    public JsonResult<?> saveOrderFB(){
        return JsonResult.err().msg("调用订单失败");
    }
}

